package com.example.lotterysystem.dao.mapper;

import com.example.lotterysystem.dao.dataobject.Encrypt;
import com.example.lotterysystem.dao.dataobject.UserDO;
import com.example.lotterysystem.dao.dataobject.WinningRecordDO;
import com.example.lotterysystem.service.dto.UserDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.stream.DoubleStream;
import java.util.stream.Stream;

@Mapper
public interface UserMapper {

    /**
     * 查询邮箱绑定的人数
     * @param email
     * @return
     */
    @Select("select count(*) from user where email = #{email}")
    int countByMail(@Param("email") String email);

    @Select("select count(*) from user where phone_number = #{phoneNumber}")
    int countByPhone(@Param("phoneNumber") Encrypt phoneNumber);

    @Insert("insert into user (user_name, email, phone_number, password, identity) " +
            " values(#{userName}, #{email}, #{phoneNumber}, #{password}, #{identity})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void insert(UserDO userDO);

    @Select("select * from user where email = #{email}")
    UserDO selectByMail(@Param("email") String email);

    @Select("select * from user where phone_number = #{phoneNumber}")
    UserDO selectByPhoneNumber(@Param("phoneNumber") Encrypt phoneNumber);

    @Select("<script>" +
            "select * from user " +
            " <if test=\"identity!=null\">" +
            "     where identity = #{identity} " +
                " <if test=\"name!=null\"> " +
                "   and user_name like concat('%', #{name} ,'%') " +
                " </if>" +
            " </if>" +
            " <if test=\"identity==null\">" +
                " <if test=\"name!=null\"> " +
                "   where user_name like concat('%', #{name} ,'%') " +
                " </if>" +
            " </if>" +
            " order by id desc " +
            "</script>")
    List<UserDO> selectUserListByIdentity(@Param("identity") String identity,@Param("name") String name);

    @Select("<script>" +
            " select id from user " +
            "   where id in " +
            "       <foreach item='item' collection='items' open = '(' separator = ',' close = ')'> " +
            "           #{item}" +
            "       </foreach>" +
            "</script>")
    List<Long> selectExistByIds(@Param("items") List<Long> ids);

    @Select("<script>" +
            " select * from user " +
            "   where id in " +
            "       <foreach item='item' collection='items' open = '(' separator = ',' close = ')'> " +
            "           #{item}" +
            "       </foreach>" +
            "</script>")
    List<UserDO> batchSelectByIds(@Param("items") List<Long> ids);

    @Select("select * from winning_record where winner_id = #{userId}")
    List<WinningRecordDO> selectWinningRecord(Integer userId);

    @Delete("delete from user where id = #{id}")
    Integer deleteByUserId(@Param("id") Long userId);

    @Select("select * from user where id = #{userId}")
    UserDO selectByUserId(@Param("userId") Long userId);

    @Update("update user set user_name = #{userName}, email = #{email}, phone_number = #{phoneNumber} " +
            "where id = #{id}")
    Integer update(UserDO userDO);
}
